package org.fve.ws;

import java.util.Hashtable;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.websocket.WebSocket.Connection;

public class WebSocketServer {
	
	private static Hashtable<String, Connection> tbuserwebsocket = new Hashtable<String, Connection>();
	private static Hashtable<String, Connection> tbuserConn = new Hashtable<String, Connection>();
	private static Hashtable<Connection, String> tbConnuser = new Hashtable<Connection,String>();
	

	public static void main(String[] args) {
		int port = 8887;
		if (args.length == 0) {
			System.out.println("WebSocketServer--> Default port:" + port);
		} else if (args.length == 1) {
			port = Integer.parseInt(args[0]);
		} else {
			System.out.println("WebSocketServer--> Usage: java -jar <jarpath> <port>");
			System.exit(0);
		}

		try {
			// 1) Create a Jetty server with the 8091 port.

			Server server = new Server(port);
			// 2) Register ChatWebSocketHandler in the Jetty server instance.
			WebSocketServerHandler chatWebSocketHandler = new WebSocketServerHandler();
			chatWebSocketHandler.setHandler(new DefaultHandler());
			server.setAttribute("tbuserwebsocket", tbuserwebsocket);
			server.setAttribute("tbuserConn", tbuserConn);
			server.setAttribute("tbConnuser", tbConnuser);
			server.setHandler(chatWebSocketHandler);
			// 2) Start the Jetty server.
			server.start();
			// Jetty server is stopped when the Thread is interruped.
			server.join();
		} catch (Throwable e) {
			e.printStackTrace();
		}

	}
}
